package com.woniuxy.stage3demo.controller.otherPzc;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.otherPzc.AdsBean;
import com.woniuxy.stage3demo.bean.otherPzc.QainforBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.otherPzc.IQainforService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RequestMapping("/qainfor")
@RestController
public class QainforController {

    @Resource
    private IQainforService qainforServiceImpl;

    @RequestMapping(value = "/page",produces = {"application/json;charset=utf-8"})
    public TableResult<QainforBean> findAllByParams(PageRequest request, QainforBean qainforBean){
        PageInfo<QainforBean> pageInfo = qainforServiceImpl.findAllByParams(request, qainforBean);
        return new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }
    @PutMapping(value = "/status/{id}/{status}",produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null && status != null) {
            QainforBean qainforBean = new QainforBean();
            qainforBean.setId(id);
            qainforBean.setStatus(status);
            qainforServiceImpl.updateQainfor(qainforBean);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg qaInforDel(@PathVariable("id") Long id){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null) {
            qainforServiceImpl.deleteQainfor(id.intValue());
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    @PutMapping(value = "/infor",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(@RequestBody QainforBean qainforBean){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(qainforServiceImpl.updateQainfor(qainforBean) == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    @PutMapping(value = "/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg save(@RequestBody QainforBean qainforBean){
        qainforBean.setQaNo("qa"+new Date().getTime());
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(qainforServiceImpl.saveQainfor(qainforBean) == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    @PutMapping(value = "/delbyids",produces = {"application/json;charset=utf-8"})
    public ResultMsg delByIds(@RequestBody Long[] ids){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(ids != null) {
            for(int i = 0;i<ids.length;i++){
                qainforServiceImpl.deleteQainfor(ids[i].intValue());
            }
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
}
